/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package co.umb.MAS.Controller;

import co.umb.MAS.Models.ConnectionDB;
import co.umb.MAS.Models.DAOs.FactoryDao;
import co.umb.MAS.Models.DAOs.PersonaDAO;
import co.umb.MAS.Models.DAOs.PostgresPersonaDao;
import co.umb.MAS.Models.PersonaModel;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Alexander
 */
public class CrearPaciente extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ConnectionDB db = new ConnectionDB();
        
        
        
        FactoryDao f  = FactoryDao.getFactoryDao(FactoryDao.POSTGRES_FACTORY_DAO);
        PostgresPersonaDao ps = (PostgresPersonaDao)f.getPersonaDao();
        PersonaModel p = new PersonaModel();
        p.setNombre(request.getParameter("txtNombre"));
        p.setApellido1(request.getParameter("txtApellido1"));
        p.setApellido2(request.getParameter("txtApellido2"));
        p.setTipoId(Integer.parseInt(request.getParameter("radioTipoID")));
        p.setNumeroID(request.getParameter("txtNumeroID"));
        p.setId_genero(Integer.parseInt(request.getParameter("radioGenero")));
//        p.setDireccion(request.getParameter("txtDireccion"));o
//        p.setCiudad(request.getParameter("txtCiudad"));
//        p.setBarrio(request.getParameter("txtBarrio"));
       String fecha = request.getParameter("txtFechaNacimiento");
       String [] div = fecha.split("/");
       if(Integer.parseInt(div[1])==12)
           div[1]="0";
       
       Calendar c = Calendar.getInstance();
       c.set(Integer.parseInt(div[0]),Integer.parseInt(div[1]),
                              Integer.parseInt(div[2]));   
       p.setFecha_nacimiento(c.getTime());
       
       Calendar cl = Calendar.getInstance();
       cl.setTime(p.getFecha_nacimiento());
       cl.get(Calendar.DAY_OF_MONTH);
       cl.get(Calendar.YEAR);
       cl.get(Calendar.MONTH);
       
        
       
                
//        p.setNumeroTelefonico(request.getParameter("txtNumeroTelefonico"));
//        p.setNumeroMovil(request.getParameter("txtNumeroMovil"));
//        p.setEmail(request.getParameter("txtEmail"));
        p.setId_rh(Integer.parseInt(request.getParameter("selectRH")));
        
        request.setAttribute("persona", p);
        
        
        if(ps.crear(p)){            
            request.getRequestDispatcher("ConsultaPaciente.do").forward(request, response);
        }    
        else{
            request.getRequestDispatcher("pages/support/sinExito.jsp").forward(request, response);
        }        
    }
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
